package person;

import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.MySQLClient;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.jdbc.impl.JDBCClientImpl;
import io.vertx.ext.sql.SQLClient;
import io.vertx.ext.sql.SQLConnection;
import io.vertx.ext.unit.Async;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.fail;

/**
 * @Author Ldj
 * @Date 2017/11/2 16:01
 * @Description 数据处理
 */
public class PersonServiceImpl implements PersonService {

    private JDBCClient client;

    public PersonServiceImpl(Vertx vertx, JsonObject config) {
        //String aaa = config.getString("server.port");
        System.out.println("------aaa-----"+config.getString("server.port"));
        JDBCClient client = JDBCClient.createShared(vertx,new JsonObject()
                .put("url", "jdbc:mysql://localhost/person?useUnicode=true&characterEncoding=utf-8")
                .put("driver_class", "com.mysql.jdbc.Driver")
                .put("max_pool_size", 30)
                .put("user", "root")
                .put("password", "yzf123!@#")
               );
    }

    @Override
    public Future<Object> apiAddPerson(Vertx vertx,String personId) {
        Future<Object> future =  Future.future();
       /* JsonObject mySQLClientConfig = new JsonObject()
                .put("host", "localhost")
                .put("username","root")
                .put("password","yzf123!@#")
                .put("port",3306)
                .put("charset","utf-8")
                .put("database","person");
       SQLClient mySQLClient = MySQLClient.createShared(vertx, mySQLClientConfig, "MySQLPool1");
*/

        List list = new ArrayList();
        JDBCClient mySQLClient = JDBCClient.createShared(vertx,new JsonObject()
                .put("url", "jdbc:mysql://localhost/person?useUnicode=true&characterEncoding=utf-8")
                .put("driver_class", "com.mysql.jdbc.Driver")
                .put("max_pool_size", 30)
                .put("user", "root")
                .put("password", "yzf123!@#")
        );
        mySQLClient.getConnection(conn->{
            if(conn.failed()){
                System.err.println(conn.cause().getMessage());
                return;
            }
            final SQLConnection connection = conn.result();
            connection.query("select * from person",rs->{
                if(rs.succeeded()){
                    for (JsonArray line : rs.result().getResults()) {
                        System.out.println(line.encode());
                        list.add(line.encode());
                    }
                    future.complete(rs.result().getResults());
                }
                else {
                    future.failed();
                }

                // and close the connection
                connection.close(done -> {
                    if (done.failed()) {
                        throw new RuntimeException(done.cause());
                    }
                });

            });
        });
        return Future.succeededFuture(list);
    }
}
